{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Azure API 提供一张含有>=3张脸（3个以上faceid）的图片，先用json()获取内容（results），在用数据结构查询的方式查找到：眼镜、肤色、微笑指数、头发颜色、年龄、性别这几项数据，并分别存进变量/自定义数据结构（如字典等）当中。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ①获取内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "subscription_key = \"eaa7f2d3c7e4456fbcc2884e463e77c0\"\n",
    "detect_url = \"https://api2.cognitiveservices.azure.com/face/v1.0/detect\"\n",
    "headers = {\n",
    "    \"Content-Type\":\"application/json\",\n",
    "    \"Ocp-Apim-Subscription-Key\":subscription_key}\n",
    "requests_img = \"https://wx4.sinaimg.cn/mw690/005K94xPgy1gk71xs3dyaj32yo1o01ky.jpg\"\n",
    "params = {\n",
    "    \"returnFaceId\":'true',\n",
    "    \"returnFaceLandmarks\":'false',\n",
    "    \"returnRecognitionModel\":\"false\",\n",
    "    \"returnFaceAttributes\":'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "response = requests.post(detect_url,headers=headers,params=params,json={\"url\":requests_img})\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '33b06123-7b38-4182-85a1-e7a0ae57ddc9',\n",
       "  'faceRectangle': {'top': 122, 'left': 121, 'width': 141, 'height': 141},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': -1.8, 'roll': 9.4, 'yaw': 9.0},\n",
       "   'gender': 'female',\n",
       "   'age': 21.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.998,\n",
       "    'sadness': 0.002,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.06},\n",
       "   'exposure': {'exposureLevel': 'overExposure', 'value': 0.79},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.61},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.22,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'other', 'confidence': 0.7},\n",
       "     {'color': 'gray', 'confidence': 0.6},\n",
       "     {'color': 'brown', 'confidence': 0.36},\n",
       "     {'color': 'blond', 'confidence': 0.03},\n",
       "     {'color': 'red', 'confidence': 0.02},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}},\n",
       " {'faceId': '971791ca-37cb-4436-a430-20a50eed4c46',\n",
       "  'faceRectangle': {'top': 153, 'left': 488, 'width': 134, 'height': 134},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 1.8, 'roll': 3.5, 'yaw': -11.0},\n",
       "   'gender': 'female',\n",
       "   'age': 16.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.999,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.74},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [{'type': 'glasses', 'confidence': 1.0}],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.21,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 0.9},\n",
       "     {'color': 'brown', 'confidence': 0.88},\n",
       "     {'color': 'other', 'confidence': 0.35},\n",
       "     {'color': 'gray', 'confidence': 0.33},\n",
       "     {'color': 'blond', 'confidence': 0.26},\n",
       "     {'color': 'red', 'confidence': 0.15},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}},\n",
       " {'faceId': '1e4d9a09-8bbd-44ea-a977-153193439c09',\n",
       "  'faceRectangle': {'top': 147, 'left': 323, 'width': 44, 'height': 44},\n",
       "  'faceAttributes': {'smile': 0.344,\n",
       "   'headPose': {'pitch': -4.1, 'roll': 1.1, 'yaw': 9.0},\n",
       "   'gender': 'female',\n",
       "   'age': 24.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.344,\n",
       "    'neutral': 0.656,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.58},\n",
       "   'noise': {'noiseLevel': 'high', 'value': 0.81},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.15,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'brown', 'confidence': 0.59},\n",
       "     {'color': 'gray', 'confidence': 0.55},\n",
       "     {'color': 'other', 'confidence': 0.48},\n",
       "     {'color': 'blond', 'confidence': 0.03},\n",
       "     {'color': 'red', 'confidence': 0.0},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}},\n",
       " {'faceId': 'bd9d9cb4-e9ff-47d2-b140-7d51592e69c2',\n",
       "  'faceRectangle': {'top': 168, 'left': 369, 'width': 43, 'height': 43},\n",
       "  'faceAttributes': {'smile': 0.001,\n",
       "   'headPose': {'pitch': -5.3, 'roll': -2.0, 'yaw': -11.0},\n",
       "   'gender': 'female',\n",
       "   'age': 22.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.001,\n",
       "    'neutral': 0.999,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.24},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.58},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.07},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.04,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'gray', 'confidence': 0.81},\n",
       "     {'color': 'other', 'confidence': 0.59},\n",
       "     {'color': 'brown', 'confidence': 0.51},\n",
       "     {'color': 'blond', 'confidence': 0.01},\n",
       "     {'color': 'red', 'confidence': 0.01},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 赋值\n",
    "face_all=[{'faceId': '33b06123-7b38-4182-85a1-e7a0ae57ddc9',\n",
    "  'faceRectangle': {'top': 122, 'left': 121, 'width': 141, 'height': 141},\n",
    "  'faceAttributes': {'smile': 0.0,\n",
    "   'headPose': {'pitch': -1.8, 'roll': 9.4, 'yaw': 9.0},\n",
    "   'gender': 'female',\n",
    "   'age': 21.0,\n",
    "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
    "   'glasses': 'NoGlasses',\n",
    "   'emotion': {'anger': 0.0,\n",
    "    'contempt': 0.0,\n",
    "    'disgust': 0.0,\n",
    "    'fear': 0.0,\n",
    "    'happiness': 0.0,\n",
    "    'neutral': 0.998,\n",
    "    'sadness': 0.002,\n",
    "    'surprise': 0.0},\n",
    "   'blur': {'blurLevel': 'low', 'value': 0.06},\n",
    "   'exposure': {'exposureLevel': 'overExposure', 'value': 0.79},\n",
    "   'noise': {'noiseLevel': 'medium', 'value': 0.61},\n",
    "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
    "   'accessories': [],\n",
    "   'occlusion': {'foreheadOccluded': False,\n",
    "    'eyeOccluded': False,\n",
    "    'mouthOccluded': False},\n",
    "   'hair': {'bald': 0.22,\n",
    "    'invisible': False,\n",
    "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
    "     {'color': 'other', 'confidence': 0.7},\n",
    "     {'color': 'gray', 'confidence': 0.6},\n",
    "     {'color': 'brown', 'confidence': 0.36},\n",
    "     {'color': 'blond', 'confidence': 0.03},\n",
    "     {'color': 'red', 'confidence': 0.02},\n",
    "     {'color': 'white', 'confidence': 0.0}]}}},\n",
    " {'faceId': '971791ca-37cb-4436-a430-20a50eed4c46',\n",
    "  'faceRectangle': {'top': 153, 'left': 488, 'width': 134, 'height': 134},\n",
    "  'faceAttributes': {'smile': 0.0,\n",
    "   'headPose': {'pitch': 1.8, 'roll': 3.5, 'yaw': -11.0},\n",
    "   'gender': 'female',\n",
    "   'age': 16.0,\n",
    "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
    "   'glasses': 'ReadingGlasses',\n",
    "   'emotion': {'anger': 0.0,\n",
    "    'contempt': 0.0,\n",
    "    'disgust': 0.0,\n",
    "    'fear': 0.0,\n",
    "    'happiness': 0.0,\n",
    "    'neutral': 0.999,\n",
    "    'sadness': 0.0,\n",
    "    'surprise': 0.0},\n",
    "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
    "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.74},\n",
    "   'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
    "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
    "   'accessories': [{'type': 'glasses', 'confidence': 1.0}],\n",
    "   'occlusion': {'foreheadOccluded': False,\n",
    "    'eyeOccluded': False,\n",
    "    'mouthOccluded': False},\n",
    "   'hair': {'bald': 0.21,\n",
    "    'invisible': False,\n",
    "    'hairColor': [{'color': 'black', 'confidence': 0.9},\n",
    "     {'color': 'brown', 'confidence': 0.88},\n",
    "     {'color': 'other', 'confidence': 0.35},\n",
    "     {'color': 'gray', 'confidence': 0.33},\n",
    "     {'color': 'blond', 'confidence': 0.26},\n",
    "     {'color': 'red', 'confidence': 0.15},\n",
    "     {'color': 'white', 'confidence': 0.0}]}}},\n",
    " {'faceId': '1e4d9a09-8bbd-44ea-a977-153193439c09',\n",
    "  'faceRectangle': {'top': 147, 'left': 323, 'width': 44, 'height': 44},\n",
    "  'faceAttributes': {'smile': 0.344,\n",
    "   'headPose': {'pitch': -4.1, 'roll': 1.1, 'yaw': 9.0},\n",
    "   'gender': 'female',\n",
    "   'age': 24.0,\n",
    "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
    "   'glasses': 'NoGlasses',\n",
    "   'emotion': {'anger': 0.0,\n",
    "    'contempt': 0.0,\n",
    "    'disgust': 0.0,\n",
    "    'fear': 0.0,\n",
    "    'happiness': 0.344,\n",
    "    'neutral': 0.656,\n",
    "    'sadness': 0.0,\n",
    "    'surprise': 0.0},\n",
    "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
    "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.58},\n",
    "   'noise': {'noiseLevel': 'high', 'value': 0.81},\n",
    "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
    "   'accessories': [],\n",
    "   'occlusion': {'foreheadOccluded': False,\n",
    "    'eyeOccluded': False,\n",
    "    'mouthOccluded': False},\n",
    "   'hair': {'bald': 0.15,\n",
    "    'invisible': False,\n",
    "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
    "     {'color': 'brown', 'confidence': 0.59},\n",
    "     {'color': 'gray', 'confidence': 0.55},\n",
    "     {'color': 'other', 'confidence': 0.48},\n",
    "     {'color': 'blond', 'confidence': 0.03},\n",
    "     {'color': 'red', 'confidence': 0.0},\n",
    "     {'color': 'white', 'confidence': 0.0}]}}},\n",
    " {'faceId': 'bd9d9cb4-e9ff-47d2-b140-7d51592e69c2',\n",
    "  'faceRectangle': {'top': 168, 'left': 369, 'width': 43, 'height': 43},\n",
    "  'faceAttributes': {'smile': 0.001,\n",
    "   'headPose': {'pitch': -5.3, 'roll': -2.0, 'yaw': -11.0},\n",
    "   'gender': 'female',\n",
    "   'age': 22.0,\n",
    "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
    "   'glasses': 'NoGlasses',\n",
    "   'emotion': {'anger': 0.0,\n",
    "    'contempt': 0.0,\n",
    "    'disgust': 0.0,\n",
    "    'fear': 0.0,\n",
    "    'happiness': 0.001,\n",
    "    'neutral': 0.999,\n",
    "    'sadness': 0.0,\n",
    "    'surprise': 0.0},\n",
    "   'blur': {'blurLevel': 'low', 'value': 0.24},\n",
    "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.58},\n",
    "   'noise': {'noiseLevel': 'low', 'value': 0.07},\n",
    "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
    "   'accessories': [],\n",
    "   'occlusion': {'foreheadOccluded': False,\n",
    "    'eyeOccluded': False,\n",
    "    'mouthOccluded': False},\n",
    "   'hair': {'bald': 0.04,\n",
    "    'invisible': False,\n",
    "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
    "     {'color': 'gray', 'confidence': 0.81},\n",
    "     {'color': 'other', 'confidence': 0.59},\n",
    "     {'color': 'brown', 'confidence': 0.51},\n",
    "     {'color': 'blond', 'confidence': 0.01},\n",
    "     {'color': 'red', 'confidence': 0.01},\n",
    "     {'color': 'white', 'confidence': 0.0}]}}}]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ②创建相关调用的数据的空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "glasses = {}\n",
    "smile = {}  \n",
    "hairColor = {}  \n",
    "age = {}  \n",
    "gender = {}  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ③将人脸的值添加入空字典内"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "while count < len(face_all):  \n",
    "    glasses[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['glasses']\n",
    "    smile[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['smile']\n",
    "    hairColor[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['hair']['hairColor'][0]['color']\n",
    "    age[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['age']\n",
    "    gender[\"Person{}\".format(count + 1)] =face_all[count]['faceAttributes']['gender']\n",
    "    count += 1  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ④打印字典输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄： {'Person1': 21.0, 'Person2': 16.0, 'Person3': 24.0, 'Person4': 22.0}\n",
      "性别： {'Person1': 'female', 'Person2': 'female', 'Person3': 'female', 'Person4': 'female'}\n",
      "眼镜： {'Person1': 'NoGlasses', 'Person2': 'ReadingGlasses', 'Person3': 'NoGlasses', 'Person4': 'NoGlasses'}\n",
      "微笑指数： {'Person1': 0.0, 'Person2': 0.0, 'Person3': 0.344, 'Person4': 0.001}\n",
      "头发颜色： {'Person1': 'black', 'Person2': 'black', 'Person3': 'black', 'Person4': 'black'}\n"
     ]
    }
   ],
   "source": [
    "print(\"年龄：\", age)\n",
    "print(\"性别：\", gender)  \n",
    "print(\"眼镜：\", glasses)\n",
    "print(\"微笑指数：\", smile)\n",
    "print(\"头发颜色：\", hairColor)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
